package com.bkgy.controller.tcmReport.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.tcmReport.entity.TbTcmEffOpenParams;
import com.bkgy.controller.tcmReport.mapper.ITbTcmEffBeginParamsMapper;
import com.bkgy.controller.tcmReport.service.ITbTcmEffOpenParamsService;
import com.bkgy.sys.common.entity.ResultData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TbTcmEffOpenParamsServiceImpl extends ServiceImpl<ITbTcmEffBeginParamsMapper, TbTcmEffOpenParams> implements ITbTcmEffOpenParamsService {
    private final ITbTcmEffBeginParamsMapper mapper;

    @Override
    public ResultData<String> buildKaijuanTimeAvg() {
        //查询开卷分档
        List<TbTcmEffOpenParams> list = mapper.selectHisEff();
        //按分档进行分组
        Map<String, List<TbTcmEffOpenParams>> map = new HashMap<>();
        for (TbTcmEffOpenParams t : list) {
            String key = t.getYpClass() + "_" + t.getExthkClass() + "_" + t.getExwidthClass() + "_" + t.getWeightUnit();
            if (!map.containsKey(key)) {
                List<TbTcmEffOpenParams> l = new ArrayList<>();
                l.add(t);
                map.put(key, l);
            } else {
                map.get(key).add(t);
            }
        }

        List<TbTcmEffOpenParams> resList = new ArrayList<>();
        //分档统计开卷4个步骤平均值
        for (Map.Entry<String, List<TbTcmEffOpenParams>> entry : map.entrySet()) {
            Integer carToExpandTime = 0;
            Integer expandToStraightTime = 0;
            Integer straightToCutterTime = 0;
            Integer cutterToWaitTime = 0;

            List<TbTcmEffOpenParams> ll = entry.getValue();
            for (TbTcmEffOpenParams t : ll) {
                carToExpandTime += t.getCarToExpandTime();
                expandToStraightTime += t.getExpandToStraightTime();
                straightToCutterTime += t.getStraightToCutterTime();
                cutterToWaitTime += t.getCutterToWaitTime();
            }
            TbTcmEffOpenParams p = new TbTcmEffOpenParams();
            String[] arr = entry.getKey().split("_");
            p.setYpClass(arr[0]);
            p.setExthkClass(arr[1]);
            p.setExwidthClass(arr[2]);
            p.setWeightUnit(arr[3]);
            p.setCarToExpandTime(carToExpandTime / ll.size());
            p.setExpandToStraightTime(expandToStraightTime / ll.size());
            p.setStraightToCutterTime(straightToCutterTime / ll.size());
            p.setCutterToWaitTime(cutterToWaitTime / ll.size());
            resList.add(p);
        }

        saveBatch(resList);
        return ResultData.ResultDataSuccess("SUCCESS");
    }
}
